package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyTxl;
import cn.stylefeng.guns.sys.modular.system.entity.BtyTxlRY;
import cn.stylefeng.guns.sys.modular.system.service.BtyTxlService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 通讯录管理控制器
 *
 * @author 张凯
 * @date 2020/8/5
 */
@Controller
@RequestMapping("/btyTxl")
public class BtyTxlController extends BaseController {

    private static final String PREFIX = "/modular/system/btyTxl/";
    //返回值
    protected static SuccessResponseData SUCCESS_TIP = new SuccessResponseData();
    //定义全局变量用来传餐厅的ID
    Long ct_id;

    @Autowired
    private BtyTxlService btyTxlService;


    /**
     * 跳转到通讯录列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyTxl.html";
    }


    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String ctbh, String dm, String csqy, String yyjl, String dd) {
        //获取用户的帐号
        String account = LoginContextHolder.getContext().getUser().getAccount();
        Page<Map<String, Object>> list = this.btyTxlService.list(ctbh, dm, csqy, yyjl, dd);

        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        this.btyTxlService.delete(id);
        return SUCCESS_TIP;
    }


    /**
     * 跳转到修改页面  没用到人员表
     * 没用到人员表的情况下 只修改餐厅表
     *
     * @return
     */
    @RequestMapping(value = "/btyTxl_update/{id}")
    public String btyTxl_update(@PathVariable Long id, Model model) {
        BtyTxl btyTxl = this.btyTxlService.getByID(id);
        model.addAllAttributes(BeanUtil.beanToMap(btyTxl));
        LogObjectHolder.me().set(btyTxl);
        return PREFIX + "btyTxl_editCT.html";
    }

    /**
     * 开始修改
     */
    @ResponseBody
    @RequestMapping(value = "/update")
    public Object update(BtyTxl btyTxl) {
        if (ToolUtil.isOneEmpty(btyTxl, btyTxl.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.btyTxlService.updateTxl(btyTxl);
        return SUCCESS_TIP;
    }

    /**
     * 获取通讯录导出excel列表(不分页)
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public Object export(String ctbh, String dm, String csqy) {
        return this.btyTxlService.listExport(ctbh, dm, csqy);
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping(value = "/btyTxl_addct")
    public String btyTxl_addct() {
        return PREFIX + "btyTxl_addct.html";
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping(value = "/btyTxl_addfzr")
    public String btyTxl_addfzr() {
        return PREFIX + "btyTxl_addfzr.html";
    }

    @RequestMapping(value = "/addfzr")
    public void addfzr(BtyTxlRY btyTxlRY) {
        this.btyTxlService.addfzr(btyTxlRY);
    }

    @RequestMapping(value = "/addct")
    public void addct(BtyTxl btyTxl) {
        this.btyTxlService.addct(btyTxl);
    }

    @RequestMapping(value = "/btyTxl_view/{id}")
    public String btyTxl_view(@PathVariable Long id) {
        ct_id = id;
        return PREFIX + "btyTxl_view.html";
    }

    @RequestMapping(value = "/view")
    @ResponseBody
    public Object view() {
        Page<Map<String, Object>> list = this.btyTxlService.view(ct_id);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    @RequestMapping(value = "/view_delete")
    @ResponseBody
    public Object view_delete(@RequestParam Long pid) {
        this.btyTxlService.view_delete(pid);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/view_update_getByID/{pid}")
    public String view_update_getByID(@PathVariable Long pid, Model model) {
        BtyTxlRY btyTxlRY = this.btyTxlService.view_update_getByID(pid);
        model.addAllAttributes(BeanUtil.beanToMap(btyTxlRY));
        LogObjectHolder.me().set(btyTxlRY);
        return PREFIX + "btyTxl_view_edit.html";
    }

    @RequestMapping(value = "/view_update")
    @ResponseBody
    public Object view_update(BtyTxlRY btyTxlRY) {
        if (ToolUtil.isOneEmpty(btyTxlRY, btyTxlRY.getPid())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.btyTxlService.view_update(btyTxlRY);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/selectTerm")
    @ResponseBody
    public Object selectDD(String term){
        List<BtyTxl> list = this.btyTxlService.selectTerm(term);
        System.out.println(list.toString());
        return list;
    }
}
